const timeExp = /\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g;

export function parseLyric(lrc) {
  if (!lrc) return [];
  const lines = lrc.split("\n");
  const result = [];
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    if (!line) continue;

    // 重置正则表达式的 lastIndex
    timeExp.lastIndex = 0;
    const match = timeExp.exec(line);

    if (match) {
      // 解析时间
      const minutes = parseInt(match[1], 10);
      const seconds = parseInt(match[2], 10);
      const milliseconds = match[3] ? parseInt(match[3].padEnd(3, "0"), 10) : 0;
      const time = minutes * 60 + seconds + milliseconds / 1000;

      // 获取歌词文本
      const text = line.replace(timeExp, "").trim();

      if (text || result.length > 0) {
        result.push({ time, text });
      }
    }
  }
  return result;
}
